/**   
 *    
 * @file
 * @brief
 * @文件名	: ShowPhotoWidthLoadingCircle
 * @版权所有	: 网龙应用软件开发一部
 * @作  者	: chenqian
 * @创建时间	: 2014-7-16 下午5:19:40 
 * @文件描述	:  
 * @version	Ver 1.1
 */
package com.common.widget.ShowBigImage;
import com.common.allCommon.CommUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

/**
 * 负责显示带有加载圆圈的图片 <br>
 * Created 2014-7-16 下午5:19:40
 * 
 * @version
 * @author chenqian
 * 
 * @see
 */

public class ShowImageWidthLoadingCircle {
	private ImageLoader mImageLoader;
	private IJudgeGif mJudgeGif;
	private DisplayImageOptions mDisplayOpt;

	/**
	 * Creates a new instance of ShowImageWidthLoadingCircle. <br>
	 * Created 2014-7-17 下午2:33:54
	 * 
	 * @param displayOpt
	 *            DisplayImageOptions
	 */
	public ShowImageWidthLoadingCircle(DisplayImageOptions displayOpt) {
		mImageLoader = ImageLoader.getInstance();
		mDisplayOpt = displayOpt;
	}

	/**
	 * Creates a new instance of ShowImageWidthLoadingCircle. <br>
	 * Created 2014-7-17 下午2:34:02
	 * 
	 * @param displayOpt
	 *            DisplayImageOptions
	 * @param judgeGif
	 *            IJudgeGif
	 */
	public ShowImageWidthLoadingCircle(DisplayImageOptions displayOpt,
			IJudgeGif judgeGif) {
		this(displayOpt);
		mJudgeGif = judgeGif;
	}

	/**
	 * 设置IJudgeGif
	 * 
	 * <br>
	 * Created 2014-7-17 下午2:34:15
	 * 
	 * @param judgeGif
	 *            IJudgeGif
	 * @author chenqian
	 */
	public void setJudgeGif(IJudgeGif judgeGif) {
		mJudgeGif = judgeGif;
	}

	/**
	 * 显示图片
	 * 
	 * <br>
	 * Created 2014-7-17 下午3:57:43
	 * 
	 * @param imageWithLoadingCircle
	 *            ImageWithLoadingCircle
	 * @param thumbUrl
	 *            缩略图地址
	 * @param sourceUrl
	 *            原图地址
	 * @param ext
	 *            图片扩展名
	 * @param rid
	 *            默认资源id
	 * @param isLocal
	 *            是否本地图
	 * @param listener
	 *            ImageLoadingListener
	 * @author chenqian
	 */
	public void displayImageOrGif(
			ImageWithLoadingCircle imageWithLoadingCircle, String thumbUrl,
			String sourceUrl, String ext, int rid, boolean isLocal,
			ImageLoadingListener listener) {
		if (null == imageWithLoadingCircle) {
			return;
		}
		if (null == mJudgeGif) {
			mJudgeGif = new DefaultJudgeGif();
		}
		if (null == listener) {
			listener = new ImageLoaderWithGifProgressListener(sourceUrl,
					imageWithLoadingCircle,mDisplayOpt,mJudgeGif,0);
		}
		if (isLocal) {
			if (mJudgeGif.isGif(sourceUrl, ext, true)) {
				imageWithLoadingCircle.showGif(sourceUrl, rid);
				return;
			}
			// 如果是本地的普通图，必须加上"file://"字段才能被imageloader加载
			sourceUrl = CommUtil.LOCAL_FILE_PREFIX + sourceUrl;
			thumbUrl = CommUtil.LOCAL_FILE_PREFIX + thumbUrl;
		} else if (mJudgeGif.isGif(sourceUrl, ext, true)) {
			imageWithLoadingCircle.showGif(mImageLoader
					.findCachedBitmapForImageUri(thumbUrl));
			ImageLoader.getInstance().loadImage(
					imageWithLoadingCircle.getGifView(), sourceUrl,
					mDisplayOpt, listener);
			return;
		}
		mImageLoader.displayImage(thumbUrl, sourceUrl,
				imageWithLoadingCircle.getPhotoView(), mDisplayOpt, listener);
	}
}
